package com.bingo.cf.certification.user.api.controller;

import com.alibaba.druid.util.StringUtils;
import com.bingo.cf.certification.user.api.entity.TestUsers;
import com.bingo.cf.certification.user.api.service.ITestUsersService;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tip.restful.core.controller.TControllerBase;
import com.tip.restful.core.controller.param.query.QueryParam;
import com.tip.restful.core.controller.result.JsonResult;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Auto-Generated
 * @since 2019/1/21
 */
@RestController
@RequestMapping("/api/testUsers")
public class TestUsersController extends TControllerBase<TestUsers, ITestUsersService> {

	private TestUsers users; 
	
	@ApiOperation("新增一个测试用户")
	@RequestMapping(value = { "add" }, method = { RequestMethod.POST })
	public JsonResult<?> add(@Valid @RequestBody TestUsers users, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return this.ResultToJson(false, bindingResult.getFieldError().getDefaultMessage());
		}
		boolean flag = false;
		if(StringUtils.isEmpty(users.getId())) {
			users.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			users.setCreateddate(new Date());
			 flag = baseService.insert(users);
		}else {
			 flag = baseService.updateById(users);
		}
		if (flag) {
			return ResultToJson(true);
		} else {
			return ResultToJson(false);
		}
	}

	@ApiOperation("删除一个测试用户")
	@RequestMapping(value = { "deleteById" }, method = { RequestMethod.GET })
	public JsonResult<?> deleteById(String id) {
		boolean flag = baseService.deleteById(id);
		if (flag) {
			return ResultToJson(true);
		} else {
			return ResultToJson(false);
		}
	}

	@ApiOperation("查询测试用户列表")
	@RequestMapping(value = { "querylist" }, method = { RequestMethod.GET })
	public JsonResult<?> querylist(String json) {
		QueryParam<TestUsers> queryData = QueryParam.parseQueryData(json, this, this.entityClazz);
		List<TestUsers> list = baseService.selectList(queryData.getEw());
		return this.ResultToJson(list);
	}

	
	
	@ApiOperation("查询单个测试用户")
    @RequestMapping(value = {"getSingleById"}, method = {RequestMethod.GET})
    public JsonResult<?> getSingleById(String id) {
		users = baseService.selectById(id);
        return this.ResultToJson(users);
    }
}
